<template>
  <div class="main">
    <div class="Box">
      <div class="back" @click="back">《</div>
      <div class="contant">
        <div class="Title">
          兑换方式
          <div class="Stitle">Ways to convert</div>
        </div>
        <div class="orderBox">
          <div class="title">
            <div>选择配送方式</div>
            <div style="width: 180px;">
              <el-radio-group v-model="radio">
                <el-radio :label="3">邮寄</el-radio>
                <!-- <el-radio :label="6">自提</el-radio> -->
              </el-radio-group>
            </div>
          </div>
          <div v-if="radio == 3">
            <div class="titleBox">
              <div>联系人:</div>
              <input
                class="inputs"
                v-model="contacts"
                placeholder="请输入联系人"
              />
            </div>
            <div class="titleBox">
              <div>联系方式:</div>
              <input
                class="inputs"
                v-model="phone"
                placeholder="请输入联系方式"
              />
            </div>
            <div class="titleBox">
              <div>邮寄地址:</div>
              <input
                class="inputs"
                v-model="address"
                placeholder="请输入邮寄详细地址"
              />
            </div>
          </div>
          <div v-else>
            <div class="titleBox">
              <div>兑换地点：</div>
              <div>{{ addressdata.address }}</div>
            </div>
            <div class="titleBox">
              <div>兑换时间：</div>
              <div>{{ addressdata.addresstime }}</div>
            </div>
            <!-- <div class="titleBox">
              <div>兑换码:</div>
              <div>284WA1241S1</div>
            </div>-->
          </div>
          <div>
            <div class="tip">
              <div>*请重复确认信息以及兑换方式，确认后无法修改</div>
            </div>
          </div>
        </div>
        <div class="BottomBtn">
          <div class="txt">
            <el-button class="BottomB" @click="enterTuiHuan()">确认</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { post_exchangeGoods } from "../../api/exchangeGoods";
import { get_addressdata } from "../../api/addressdata";

export default {
  name: "ExchangeProcess",
  data() {
    return {
      activeName: "second",
      radio: 3,
      contacts: "",
      phone: "",
      address: "",
      addressdata: ""
    };
  },
  async created() {
    console.log(this.$route.query);
    var addressdata = await get_addressdata({ query: {} });
    console.log("地址", addressdata.data[0]);
    this.addressdata = addressdata.data[0];
  },
  components: {},
  methods: {
    back() {
      this.$router.go(-1); //返回上一层
    },
    SelectGoods(e) {
      console.log(e);
    },
    navigationSelect(path) {
      this.$router.push(path);
    },
    async enterTuiHuan() {
      console.log(this.contacts, this.phone, this.address);
      if (this.radio == 3) {
        if (this.contacts == "" || this.phone == "" || this.address == "") {
          this.$message({
            showClose: true,
            message: "请完善邮寄信息",
            type: "warning"
          });
          return;
        } else {
          if (!/^1[3456789]\d{9}$/.test(this.phone)) {
            this.$message({
              showClose: true,
              message: "手机号码有误，请重填",
              type: "warning"
            });
            return;
          }
        }
      }
      var exchangeData = {
        goodID: this.$route.query._id,
        userID: this.$store.getters.userInfo._id,
        exchangeTpye: this.radio == 3 ? "邮寄" : "自提",
        receiving_address: {
          contacts: this.contacts,
          phone: this.phone,
          address: this.address
        }
      };
      var re = await post_exchangeGoods(exchangeData);
      console.log(re);
      if (re.text == "兑换成功") {
        this.$message({
          showClose: true,
          message: re.text,
          type: "success"
        });
        setTimeout(() => {
          this.$router.push({
            path: "/ExchangeDetail", //跳转路径
            name: "ExchangeDetail", //跳转路径的name值，不写跳转后页面取不到参数
            // 参数
            query: {
              _id: re.exchangeId
            }
          });
        }, 2000);
      } else {
        this.$message({
          showClose: true,
          message: re.text,
          type: "warning"
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.main {
  height: 800px;
  background-color: #ffffff;
  .Box {
    width: 80%;
    min-width: 1000px;
    margin: 0px auto;
    padding: 30px 0 0 0;
    display: flex;
    flex-direction: column;
    .back {
      margin: 10px 0;
      cursor: pointer;
    }
    .Title {
      font-size: 28px;
      border-bottom: solid 1px #bbbbbb;
      height: 80px;
      font-weight: 550;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .Stitle {
        font-size: 14px;
        font-weight: 100;
        margin: 5px 0;
      }
    }
    .orderBox {
      height: 300px;
      display: flex;
      flex-direction: column;
      align-items: center;
      border-bottom: solid 1px #bbbbbb;
      .title {
        display: flex;
        width: 300px;
        margin: 20px 0 20px 0;
        justify-content: space-between;
      }
      .titleBox {
        display: flex;
        width: 280px;
        margin: 30px 0 0 0;
        justify-content: space-between;
      }
      .tip {
        display: flex;
        width: 280px;
        font-size: 13px;
        color: #c70000;
        margin: 30px 0 0 0;
        justify-content: space-between;
      }
      .inputs {
        border: 0;
        border-bottom: 1.2px solid #c7c5c5;
      }
    }
    .BottomBtn {
      width: 80%;
      height: 280px;
      margin: 0 auto;
      align-items: center;
      align-content: center;
      display: flex;
      align-content: center;
      justify-content: center;
      .txt {
        display: flex;
        flex-direction: column;
        align-content: center;
        height: 100px;
        color: saddlebrown;
        align-items: center;
        font-size: 14px;
        .BottomB {
          background-color: #1d2f70;
          color: #ffffff;
          width: 240px;
          height: 50px;
        }
      }
    }
  }
}
</style>
